<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

</head>

<body>
    <div class="wrap">
        <p>商品编号：<input id="code" type="text"></p>
        <p>商品名称：<input id="name" type="text"></p>
        <p>商品图片：<input id="img" type="text"></p>
        <p>商品数量：<input id="num" type="text"></p>
        <p>商品价格：<input id="price" type="text"></p>
        <p><input id="btn" type="submit" value="提交商品"></p>
    </div>
</body>
<script src="js/cookie.js"></script>
<script>
    // var obj = {
    //     goodsId: "A001",
    //     goodsName: "Casio/卡西欧 EX-TR350",
    //     goodsImg: "images/1.jpg",
    //     goodsNum: 1,
    //     goodsPrice: 5999.88
    // }

    var codeInp = document.querySelector("#code");
    var nameInp = document.querySelector("#name");
    var imgInp = document.querySelector("#img");
    var numInp = document.querySelector("#num");
    var priceInp = document.querySelector("#price");
    var btn = document.querySelector("#btn");

    btn.onclick = function () {
        var goodsId = codeInp.value;
        var goodsName = nameInp.value;
        var goodsImg = imgInp.value;
        var goodsNum = numInp.value;
        var goodsPrice = priceInp.value;

        // 添加之前判断对应商品是否存在

        var goodsDetail = localStorage.getItem("goods_" + goodsId);//商品的信息  (有就返回所有信息，没有返回null)

        if (goodsDetail) {//存在  修改数量
            var goods = JSON.parse(goodsDetail);  // 商品的信息转对象
            goods.goodsNum = (goods.goodsNum * 1) + (goodsNum * 1);

            localStorage.setItem("goods_" + goodsId, JSON.stringify(goods));


        } else { //不存在  添加
            var obj = {
                // 属性名：属性值
                "goodsId": goodsId,
                "goodsName": goodsName,
                "goodsImg": goodsImg,
                "goodsNum": goodsNum,
                "goodsPrice": goodsPrice
            }
            // var obj = {
            //     goodsId, goodsName, goodsImg, goodsNum, goodsPrice
            // }
            console.log(obj);

            // 添加
            localStorage.setItem("goods_" + goodsId, JSON.stringify(obj));
        }





    }

</script>

</html>